<?php
namespace app\system\model;
use app\base\model\BaseModel;
/**
 * 文件操作
 */
class SystemFileModel extends BaseModel {
    //完成
    protected $_auto = array (
        array('time','time',3,'function'),
     );

    /**
     * 上传数据
     * @return array 文件信息
     */
    public function uploadData()
    {
        $upload = target('base/Upload');
        $config = array();
        $config['DIR_NAME'] = date('Y-m-d');
        //处理图片大小
        $width = request('post.width');
        $height = request('post.height');
        if($width && $height){
            $config['THUMB_WIDTH'] = $width;
            $config['THUMB_HEIGHT'] = $height;
            $config['THUMB_STATUS'] = true;
            $config['THUMB_TYPE'] = 2;
        }
        $data = $upload->upload($config);
        if(!$data){
            $this->error = $upload->getError();
            return false;
        }
        $data['user_id'] = SYSTEM_ID;
        $this->addData('add', $data);
        return $data;
    }

    /**
     * 获取列表
     * @return array 列表
     */
    public function loadList($where = array(), $limit = 0, $order = ''){
        $data = $this->table('system_file as A')
                ->join('{pre}system_user as B ON A.user_id = B.user_id')
                ->field('A.*,B.username')
                ->where($where)
                ->limit($limit)
                ->order('A.file_id desc')
                ->select();
        return $data;

    }

    /**
     * 获取数量
     * @return int 数量
     */
    public function countList($where = array()){
        return $this->table('system_file as A')
                ->join('{pre}system_user as B ON A.user_id = B.user_id')
                ->where($where)
                ->count();
    }

    /**
     * 添加信息
     * @param string $data 增加数据
     * @return bool 更新状态
     */
    public function addData($type, $data){
        $data = $this->create($data);
        return $this->add($data);
    }

}
